<template>
  <app-dialog v-model="show" width="800px" :title="addData.title" @open="handleOpen">
    <div>
      <div class="task">
        <div class="task_label">机器人名称:</div>
        <el-input v-model="formdata.task_name" />
      </div>
      <SurebetsBasic :formdata="formdata" :grouplist="grouplist" :schemelist="schemelist" />
      <SurebetsPrice :formdata="formdata" />
    </div>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" class="hovepcvicecolor" @click="handleSubmit">确定</el-button>
    </template>
  </app-dialog>
</template>

<script setup>
import SurebetsBasic from './components/SurebetsBasic.vue'
import SurebetsPrice from './components/SurebetsPrice.vue'

import { http } from '@/utils/http'
import { ElMessage } from 'element-plus'

const show = defineModel({ type: Boolean })
const emits = defineEmits(['change'])
const props = defineProps({
  addData: Object,
})

const formdata = ref({
  task_name: '',
  scheme_code: '',
  users_config: '',
  stake_type: 'real',
  stake: 2000,
  is_superior_place: 0,
})
const grouplist = ref([])
const schemelist = ref([])

const handleClose = () => {
  show.value = false
}

const handleSubmit = () => {
  console.log(formdata.value)
  const onSuccess = () => {
    ElMessage.success('操作成功')
    handleClose()
    emits('change')
  }
  http({
    method: 'POST',
    url: '/robot/creat_or_update_place_robot',
    data: formdata.value,
  }).then(onSuccess)
}

const getgrouplist = () => {
  http.get(`/group/group_list?page=1&page_size=999&status=1`).then(res => {
    grouplist.value = res.data.list
  })
}

const getschemelist = () => {
  http.get(`/scheme/scheme_list?page=1&page_size=999`).then(res => {
    schemelist.value = res.data.list
  })
}

const handleOpen = () => {
  getgrouplist()
  getschemelist()

  if (props.addData.data.task_code == null) {
    formdata.value = {
      task_name: '',
      scheme_code: '',
      users_config: '',
      stake_type: 'real',
      stake: 2000,
      is_superior_place: 0,
    }
  } else {
    formdata.value = JSON.parse(JSON.stringify(props.addData.data.task_form))
    formdata.value.task_code = props.addData.data.task_code
  }
}
</script>

<style lang="scss" scoped>
.task {
  display: flex;
  align-items: center;

  .task_label {
    width: 108px;
  }
}
</style>